﻿using System;
using System.Collections;
using IllusionUtility.GetUtility;
using Studio;
using UnityEngine;
using Object = UnityEngine.Object;
namespace WzhAddon.DanLookAt
{
	public class DanLookAtController : MonoBehaviour
	{
		private OCICharMale studioMale;
		public GameObject targetMarker;
		public bool lookEnabled;
		private Material material;
		private bool _syncKokan;
		public AdvancedIKController ikController;
		private bool resetMarkerPos;
		public bool showMarker = true;
		public static DanLookAtController Install(OCICharMale male, AdvancedIKController ikController)
		{
			DanLookAtController danLookAtController = male.male.gameObject.GetComponent<DanLookAtController>();
			if (danLookAtController == null)
			{
				danLookAtController = male.male.gameObject.AddComponent<DanLookAtController>();
				danLookAtController.studioMale = male;
				Material material = new Material(ShaderUtil.TransparentZAlways);
				danLookAtController.material = material;
				danLookAtController.ikController = ikController;
			}
			return danLookAtController;
		}

		public static DanLookAtController Get(OCIChar male)
		{
			if (male is OCICharMale)
			{
				return DanLookAtController.Get((male as OCICharMale).male);
			}
			return null;
		}
		public static DanLookAtController Get(ChaControl maleChara)
		{
			return maleChara.gameObject.GetComponent<DanLookAtController>();
		}

		public bool SyncKokan
		{
			get
			{
				return this._syncKokan;
			}
			set
			{
				if (this._syncKokan != value)
				{
					this._syncKokan = value;
					this.UpdateBase();
				}
			}
		}

		private void Start()
		{
			this.targetMarker = GameObject.CreatePrimitive((PrimitiveType)3);
			this.targetMarker.name = "dan_target_marker";
			this.targetMarker.AddComponent<DanLookAtController.LookTarget>().controller = this;
			CharReference component = base.gameObject.GetComponent<CharReference>();
			this.targetMarker.transform.position = component.GetReferenceInfo((CharReference.RefObjKey)33).transform.position;
			this.targetMarker.transform.localScale = Vector3.one * 0.05f;
			Renderer component2 = this.targetMarker.GetComponent<Renderer>();
			component2.material = this.material;
			component2.material.color = new Color(0f, 0f, 1f, 0.3f);
			this.targetMarker.GetComponent<Collider>().isTrigger = true;
			this.targetMarker.AddComponent<Rigidbody>().isKinematic = true;
			this.targetMarker.AddComponent<ObjectScreenController2>();
			this.targetMarker.AddComponent<SetRenderQueue>();
			this.UpdateBase();
		}

		public void ShowHideMarker(bool show)
		{
			this.targetMarker.SetActive(show);
		}

		public void ResetMakerPosToAnime()
		{
			if (!this.lookEnabled)
			{
				CharReference component = base.gameObject.GetComponent<CharReference>();
				this.targetMarker.transform.position = component.GetReferenceInfo((CharReference.RefObjKey)33).transform.position;
				return;
			}
			base.StartCoroutine(this.ResetMarkerPosCo());
		}

		private IEnumerator ResetMarkerPosCo()
		{
			this.lookEnabled = false;
			yield return null;
			yield return new WaitForEndOfFrame();
			CharReference component = this.gameObject.GetComponent<CharReference>();
			this.targetMarker.transform.position = component.GetReferenceInfo((CharReference.RefObjKey)33).transform.position;
			this.lookEnabled = true;
			yield break;
		}

		private void UpdateBase()
		{
			if (this._syncKokan)
			{
				this.targetMarker.transform.parent = TransformFindEx.FindLoop(this.studioMale.male.human.objBodyBone.transform, "cm_J_Kokan").transform;
				return;
			}
			this.targetMarker.transform.parent = this.studioMale.male.gameObject.transform;
		}

		private void Update()
		{
			if (!this.lookEnabled)
			{
				this.targetMarker.SetActive(false);
				return;
			}
			this.targetMarker.SetActive(this.showMarker);
		}

		public void LateUpdate()
		{
			if (this.lookEnabled)
			{
				GameObject referenceInfo = this.studioMale.male.GetReferenceInfo((CharReference.RefObjKey)32);
				GameObject referenceInfo2 = this.studioMale.male.GetReferenceInfo((CharReference.RefObjKey)33);
				if (referenceInfo == null)
				{
					return;
				}
				if (this.targetMarker.transform == null)
				{
					return;
				}
				referenceInfo.transform.LookAt(this.targetMarker.transform);
				if (referenceInfo2 != null)
				{
					referenceInfo2.transform.position = this.targetMarker.transform.position;
				}
			}
		}

		private void Destroy()
		{
			if (this.targetMarker)
			{
				Object.Destroy(this.targetMarker);
			}
		}

	
		public class LookTarget : MonoBehaviour
		{
			public DanLookAtController controller;
		}
	}
}
